﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.Auth.Service.OAuth
{
    public class Validator
    {
        public Validator()
        {
            IsValid = true;
        }

        private Validator(bool isValid)
        {
            IsValid = isValid;
        }

        public bool IsValid { get; private set; }
        public string Error { get; private set; }
        public string ErrorDescription { get; private set; }

        public static Validator Invalid { get { return new Validator(false); } }

        public Validator When(Func<bool> condition, string errorDescription)
        {
            return When(condition, null, errorDescription);
        }

        public Validator When(Func<bool> condition, string error, string errorDescription)
        {
            if (IsValid && condition.Invoke())
            {
                IsValid = false;
                Error = error;
                ErrorDescription = errorDescription;
            }

            return this;
        }
    }
}
